<?php

namespace Parser\Separator;

use Parser\Enquiry;
use Parser\Parser;
use Parser\Repetition;

class ZFBX implements BaseSeparator
{
    public static $ApiKindList = [
        3000 => '001',
        3002 => '0011',
        30002 => '0014',
        30004 => '0016',
        4000 => '002',
        4001 => '0022',
        5002 => '0041',
        5001 => '0044',
        40002 => '0049',
        50022 => '0055',
        50012 => '0057',
    ];

    public function interpreter(array $array, $parser)
    {
        if ($this->error($array, $parser)) {
            return;
        }

        $parser->result = $this->analysis($array);
    }

    /**
     *错误解析
     * Author:ls
     *
     * @param $array
     * @param parser $parser
     * @return bool
     */
    public function error(&$array, $parser): bool
    {
        if ($compErrorMsg = $array['compErrorMsg'] ?? false) {
            $parser->message = $compErrorMsg;
            $parser->code = '500';
            return true;
        }

        if ($repeatInsu = $array['repeatInsu']['repeatMsg'] ?? false) {

            if ($lastEndDate = $array['commResult']['lastEndDate'] ?? false) {
                $repeatInsu .= "\r\n" . '该客户商业险上年保险止期为' . date('Y年m月d日 H:i:s', strtotime($lastEndDate)) . ',请核对本年商业险起保日期。';
            }
            $parser->message = $repeatInsu;
            $parser->code = '500';
            return true;
        }
        return false;
    }

    //报价解析
    public function analysis($array): array
    {

        $enquiry = new Enquiry();

        //交强险
        if ($compResult = $array['compResult'] ?? false) {
            $vci = array_pop($compResult['coverageDatas']);
            $tax = array_pop($compResult['carShipTaxSubList']);
            $enquiry->setTaxTotal($tax['annualTaxAmount']);
            $enquiry->setTaxPremium($tax['taxDue']);
            $enquiry->setTCITotal($vci['coveragePremium']);
            $enquiry->setTCIPremium($vci['basicPremium']);
        }

        //商业险
        if ($commResult = $array['commResult'] ?? false) {
            $enquiry->setVCITotal($commResult['premium']);
            $enquiry->setVCIDiscount($commResult['totalDiscount']);

            if ($coverageDatas = $commResult['coverageDatas'] ?? false) {
                foreach ($coverageDatas as $value) {
                    $enquiry->setVCIPremium(bcadd($enquiry->getVCIPremium(), $value['basicPremium']));
                    //                    $enquiry->setVCITotal(bcadd($enquiry->getVCITotal(), $value['coveragePremium']));

                    $enquiry->setVCICoverages([
                        'code' => (string)array_search($value['coverageCode'], self::$ApiKindList),
                        'shortName' => $value['coverageName'],
                        'insuredDesc' => $value['coverageAmount'],
                        'total' => $value['coveragePremium'],
                    ]);
                }

            }
            //费率因子
            if ($rates = $commResult['rate'] ?? false) {
                foreach ($rates as $rate) {
                    //自主定价系数
                    if ($profitCode = $rate['profitCode'] ?? false and $profitCode == 'F05') {
                        $enquiry->setIndependentPriceRatio(bcdiv($rate['rate'] ?? '0', 100, 6));
                    }
                    //无赔款优待系数
                    if ($profitCode = $rate['profitCode'] ?? false and $profitCode == 'F03') {
                        $enquiry->setNCDRate(bcdiv($rate['rate'] ?? '0', 100, 6));
                    }
                }
            }
        }

        $enquiry->setTotal();
        $enquiry->success();

        return $enquiry->get();
    }

    //数据解析
    public function repetition($array): Repetition
    {
        $repetition = new Repetition();

        if ($repeatMsg = $array['repeatInsu']['repeatMsg'] ?? false) {
            preg_match('/险种名称:(.*)/', $repeatMsg, $matches);
            if (!$matches) {
                return $repetition;
            }
            $coverage = explode(';', $matches[1]);
            $repetition->setLastCoverage($coverage);
            $repeatMsg = str_replace([$matches[0], '商业险重复投保提示：', '<br>',], ['', '', ''], $repeatMsg);
            $repeatMsg = explode(';', $repeatMsg);

            foreach ($repeatMsg as &$value) {
                if (!$value) {
                    break;
                }
                $value = explode('：', $value);
                $column = trim($value[0]);
                $value = trim($value[1]);
                switch ($column) {
                    case '保单号':
                        $repetition->setLastPolicy($value);
                        break;
                    case '保险公司':
                        $repetition->setLastCompany($value);
                        break;
                    case '起保日期':
                        $repetition->setLastStartAt($value);
                        break;
                    case '终保日期':
                        $repetition->setLastEndTime($value);
                        break;
                    case '签单日期':
                        $repetition->setLastSignTime($value);
                        break;
                    case "号牌号码":
                        $repetition->setLicense($value);
                        break;
                }
            }
            if ($repetition->getLastCompany()) {
                $repetition->setStatus('success');
            }
        }

        return $repetition;
    }
}

